/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.scripts.quests;

import l2s.commons.util.Rnd;
import l2s.gameserver.model.L2World;
import l2s.gameserver.model.actor.L2Npc;
import l2s.gameserver.model.actor.instance.L2PcInstance;
import l2s.gameserver.model.quest.Quest;
import l2s.gameserver.model.quest.QuestState;
import l2s.gameserver.scripting.ScriptFile;

public class Q211_TrialOfTheChallenger extends Quest implements ScriptFile
{
	private static final String qn = "Q211_TrialOfTheChallenger";
	private static final int LETTER_OF_KASH_ID = 2628;
	private static final int SCROLL_OF_SHYSLASSY_ID = 2631;
	private static final int WATCHERS_EYE1_ID = 2629;
	private static final int BROKEN_KEY_ID = 2632;
	private static final int MITHRIL_SCALE_GAITERS_MATERIAL_ID = 2918;
	private static final int BRIGANDINE_GAUNTLET_PATTERN_ID = 2927;
	private static final int MANTICOR_SKIN_GAITERS_PATTERN_ID = 1943;
	private static final int GAUNTLET_OF_REPOSE_OF_THE_SOUL_PATTERN_ID = 1946;
	private static final int IRON_BOOTS_DESIGN_ID = 1940;
	private static final int TOME_OF_BLOOD_PAGE_ID = 2030;
	private static final int ELVEN_NECKLACE_BEADS_ID = 1904;
	private static final int WHITE_TUNIC_PATTERN_ID = 1936;
	private static final int ADENA_ID = 57;
	private static final int MARK_OF_CHALLENGER_ID = 2627;
	private static final int WATCHERS_EYE2_ID = 2630;
	private static final int RewardExp = 72394;
	private static final int RewardSP = 11250;
	
	public Q211_TrialOfTheChallenger(int questId, String name, String descr)
	{
		super(questId, name, descr);
		addStartNpc(30644);
		addTalkId(30644);
		addTalkId(30644);
		addTalkId(30644);
		addTalkId(30535);
		for (int i = 30644; i < 30648; i++)
		{
			addTalkId(i);
		}
		for (int i = 27110; i < 27115; i++)
		{
			addKillId(i);
		}
		questItemIds = new int[]
		{
			SCROLL_OF_SHYSLASSY_ID,
			LETTER_OF_KASH_ID,
			WATCHERS_EYE1_ID,
			BROKEN_KEY_ID,
			WATCHERS_EYE2_ID
		};
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (event.equalsIgnoreCase("1"))
		{
			htmltext = "30644-05.htm";
			st.set("cond", "1");
			st.setState(Quest.STATE_STARTED);
			st.giveItems(7562, 64);
			st.playSound(QuestState.SOUND_ACCEPT);
		}
		else if (event.equalsIgnoreCase("30644_1"))
		{
			htmltext = "30644-04.htm";
		}
		else if (event.equalsIgnoreCase("30645_1"))
		{
			htmltext = "30645-02.htm";
			st.takeItems(LETTER_OF_KASH_ID, 1);
			st.set("cond", "4");
		}
		else if (event.equalsIgnoreCase("30647_1"))
		{
			if (st.getQuestItemsCount(BROKEN_KEY_ID) > 0)
			{
				st.giveItems(SCROLL_OF_SHYSLASSY_ID, 1);
				if (Rnd.chance(22))
				{
					htmltext = "30647-03.htm";
					st.takeItems(BROKEN_KEY_ID, 1);
					st.playSound(QuestState.SOUND_JACKPOT);
					int n = Rnd.get(100);
					if (n > 90)
					{
						st.giveItems(MITHRIL_SCALE_GAITERS_MATERIAL_ID, 1);
						st.giveItems(BRIGANDINE_GAUNTLET_PATTERN_ID, 1);
						st.giveItems(MANTICOR_SKIN_GAITERS_PATTERN_ID, 1);
						st.giveItems(GAUNTLET_OF_REPOSE_OF_THE_SOUL_PATTERN_ID, 1);
						st.giveItems(IRON_BOOTS_DESIGN_ID, 1);
					}
					else if (n > 70)
					{
						st.giveItems(TOME_OF_BLOOD_PAGE_ID, 1);
						st.giveItems(ELVEN_NECKLACE_BEADS_ID, 1);
					}
					else if (n > 40)
					{
						st.giveItems(WHITE_TUNIC_PATTERN_ID, 1);
					}
					else
					{
						st.giveItems(IRON_BOOTS_DESIGN_ID, 1);
					}
				}
				else
				{
					htmltext = "30647-02.htm";
					st.takeItems(BROKEN_KEY_ID, -1);
					st.giveItems(ADENA_ID, Rnd.get(1000) + 1);
				}
			}
			else
			{
				htmltext = "30647-04.htm";
			}
		}
		else if (event.equalsIgnoreCase("30646_1"))
		{
			htmltext = "30646-02.htm";
		}
		else if (event.equalsIgnoreCase("30646_2"))
		{
			htmltext = "30646-03.htm";
		}
		else if (event.equalsIgnoreCase("30646_3"))
		{
			htmltext = "30646-04.htm";
			st.set("cond", "8");
			st.takeItems(WATCHERS_EYE2_ID, 1);
		}
		else if (event.equalsIgnoreCase("30646_4"))
		{
			htmltext = "30646-06.htm";
			st.set("cond", "8");
			st.takeItems(WATCHERS_EYE2_ID, 1);
		}
		else if (event.equalsIgnoreCase("30646_Fail"))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(30646);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
		}
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		QuestState st = player.getQuestState(qn);
		if (st.getQuestItemsCount(MARK_OF_CHALLENGER_ID) > 0)
		{
			st.exitQuest(true);
			return "completed";
		}
		String htmltext = "noquest";
		int npcId = npc.getNpcId();
		int id = st.getState();
		int cond = st.getInt("cond");
		if (id == Quest.STATE_CREATED)
		{
			st.set("cond", "0");
			if (npcId == 30644)
			{
				if ((st.getPlayer().getClassId().ordinal() == 0x01) || (st.getPlayer().getClassId().ordinal() == 0x13) || (st.getPlayer().getClassId().ordinal() == 0x20) || (st.getPlayer().getClassId().ordinal() == 0x2d) || (st.getPlayer().getClassId().ordinal() == 0x2f))
				{
					if (st.getPlayer().getLevel() >= 35)
					{
						htmltext = "30644-03.htm";
					}
					else
					{
						htmltext = "30644-01.htm";
						st.exitQuest(true);
					}
				}
				else
				{
					htmltext = "30644-02.htm";
					st.exitQuest(true);
				}
			}
		}
		else if ((npcId == 30644) && (cond == 1))
		{
			htmltext = "30644-06.htm";
		}
		else if ((npcId == 30644) && (cond == 2) && (st.getQuestItemsCount(SCROLL_OF_SHYSLASSY_ID) == 1))
		{
			htmltext = "30644-07.htm";
			st.takeItems(SCROLL_OF_SHYSLASSY_ID, 1);
			st.giveItems(LETTER_OF_KASH_ID, 1);
			st.set("cond", "3");
		}
		else if ((npcId == 30644) && (cond == 1) && (st.getQuestItemsCount(LETTER_OF_KASH_ID) == 1))
		{
			htmltext = "30644-08.htm";
		}
		else if ((npcId == 30644) && (cond >= 7))
		{
			htmltext = "30644-09.htm";
		}
		else if ((npcId == 30645) && (cond == 3) && (st.getQuestItemsCount(LETTER_OF_KASH_ID) == 1))
		{
			htmltext = "30645-01.htm";
		}
		else if ((npcId == 30645) && (cond == 4) && (st.getQuestItemsCount(WATCHERS_EYE1_ID) == 0))
		{
			htmltext = "30645-03.htm";
		}
		else if ((npcId == 30645) && (cond == 5) && (st.getQuestItemsCount(WATCHERS_EYE1_ID) > 0))
		{
			htmltext = "30645-04.htm";
			st.takeItems(WATCHERS_EYE1_ID, 1);
			st.set("cond", "6");
		}
		else if ((npcId == 30645) && (cond == 6))
		{
			htmltext = "30645-05.htm";
		}
		else if ((npcId == 30645) && (cond >= 7))
		{
			htmltext = "30645-06.htm";
		}
		else if ((npcId == 30647) && (cond == 2))
		{
			htmltext = "30647-01.htm";
		}
		else if ((npcId == 30646) && (cond == 7) && (st.getQuestItemsCount(WATCHERS_EYE2_ID) > 0))
		{
			htmltext = "30646-01.htm";
		}
		else if ((npcId == 30646) && (cond == 8))
		{
			htmltext = "30646-06a.htm";
		}
		else if ((npcId == 30646) && (cond == 10))
		{
			L2Npc isQuest = L2World.getInstance().getNpcById(30646);
			if (isQuest != null)
			{
				isQuest.deleteMe();
			}
			htmltext = "30646-07.htm";
			st.takeItems(BROKEN_KEY_ID, -1);
			st.giveItems(MARK_OF_CHALLENGER_ID, 1);
			st.getPlayer().addExpAndSp(RewardExp, RewardSP);
			st.playSound(QuestState.SOUND_FINISH);
			st.unset("cond");
			st.exitQuest(false);
		}
		else if ((npcId == 30535) && (cond == 8))
		{
			if (st.getPlayer().getLevel() >= 36)
			{
				htmltext = "30535-01.htm";
				st.addRadar(176560, -184969, -3729);
				st.set("cond", "9");
			}
			else
			{
				htmltext = "30535-03.htm";
			}
		}
		else if ((npcId == 30535) && (cond == 9))
		{
			htmltext = "30535-02.htm";
			st.addRadar(176560, -184969, -3729);
		}
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		int npcId = npc.getNpcId();
		QuestState st = checkPlayerState(player, npc, STATE_STARTED);
		if (st == null)
		{
			return null;
		}
		int cond = st.getInt("cond");
		if ((npcId == 27110) && (cond == 1) && (st.getQuestItemsCount(SCROLL_OF_SHYSLASSY_ID) == 0) && (st.getQuestItemsCount(BROKEN_KEY_ID) == 0))
		{
			st.giveItems(BROKEN_KEY_ID, 1);
			addSpawn(30647, player, false, 0, true);
			st.playSound(QuestState.SOUND_MIDDLE);
			st.set("cond", "2");
		}
		else if ((npcId == 27112) && (cond == 4) && (st.getQuestItemsCount(WATCHERS_EYE1_ID) == 0))
		{
			st.giveItems(WATCHERS_EYE1_ID, 1);
			st.set("cond", "5");
			st.playSound(QuestState.SOUND_MIDDLE);
		}
		else if ((npcId == 27113) && (cond == 6) && (st.getQuestItemsCount(WATCHERS_EYE2_ID) == 0))
		{
			L2Npc npcspawn = L2World.getInstance().getNpcById(30646);
			if (npcspawn == null)
			{
				st.giveItems(WATCHERS_EYE2_ID, 1);
				st.playSound(QuestState.SOUND_MIDDLE);
				st.set("cond", "7");
				addSpawn(30646, player, false, 0, true);
				startQuestTimer("30646_Fail", 300000, null, st.getPlayer(), false);
			}
		}
		else if ((npcId == 27113) && (cond == 7))
		{
			L2Npc npcspawn = L2World.getInstance().getNpcById(30646);
			if (npcspawn == null)
			{
				if (st.getQuestItemsCount(WATCHERS_EYE2_ID) == 0)
				{
					st.giveItems(WATCHERS_EYE2_ID, 1);
				}
				st.playSound(QuestState.SOUND_MIDDLE);
				st.set("cond", "7");
				addSpawn(30646, player, false, 0, true);
				startQuestTimer("30646_Fail", 300000, null, st.getPlayer(), false);
			}
		}
		else if ((npcId == 27114) && ((cond == 9) || (cond == 10)))
		{
			L2Npc npcspawn = L2World.getInstance().getNpcById(30646);
			if (npcspawn == null)
			{
				st.set("cond", "10");
				st.playSound(QuestState.SOUND_MIDDLE);
				addSpawn(30646, player, false, 0, true);
				startQuestTimer("30646_Fail", 300000, null, st.getPlayer(), false);
			}
		}
		return null;
	}
	
	public static void onLoad()
	{
		new Q211_TrialOfTheChallenger(211, qn, "Trial Of Challenger");
	}
}
